home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_02 / heintze / bitarr.h < prev   
C/C++ Source or Header  |  1993-12-01  |  3KB  |  106 lines

  1. //file: BitArr.h ---------------------
  2. #ifndef BITARR_H
  3. #define BITARR_H
  4. #include "Boolean.h"
  5. #include <assert.h>
  6. #include <iostream.h>
  7.  
  8. // Class of temporary objects to reference bit fields
  9. class BitField{
  10. public:
  11.     BitField(unsigned long *data, unsigned int pos, unsigned width):
  12.       _data(data), _pos(pos), _width(width)
  13.     {
  14.       assert(_pos+_width<=32);
  15.     }
  16.     operator unsigned long(){
  17.       unsigned long mask = 0xffffffff >> (32 - _width);
  18.       unsigned long result = (*_data >> _pos) & mask;
  19.       return result;
  20.     }
  21.  
  22.     //Let the compiler supply these as necessary:
  23.     //  BitField(const BitField& src);
  24.     //  BitField& operator=(const BitField& rhs);
  25.     //  ~BitField();
  26.  
  27.     // Assignment operator.  This is the code that deposits
  28.     //   an integer value into the packed array of bits.
  29.     BitField& operator=(unsigned long rhs){
  30.       unsigned long mask = 0xffffffff >> (32 - _width);
  31.       rhs &= mask;
  32.       rhs <<= _pos;
  33.       mask <<= _pos;
  34.       *_data &= ~mask;
  35.       *_data |= rhs;
  36.       return *this;
  37.     }
  38. private:
  39.     unsigned long *_data;
  40.     int _pos;
  41.     int _width;
  42. };
  43.  
  44. class BitArray{
  45. public:
  46.     // Default Constructor.
  47.     //  Optional arguments:
  48.     //   width:  number of bits in an array element
  49.     //   val:    initial value of representation of bit array
  50.     BitArray(unsigned int width=1, unsigned long val=0):
  51.      _len(32), _data(val),_width(width){
  52.      assert(_width <= _len);
  53.     }
  54.  
  55.     //Let the compiler supply these as necessary:
  56.     //  BitArray(const BitArray& src);
  57.     //  BitArray& operator=(const BitArray& rhs);
  58.     //  ~BitArray();
  59.  
  60.     // Return the number of bits in an array element
  61.     unsigned int width() const {
  62.       return _width;
  63.     }
  64.  
  65.     // Return the index of the first element in the array
  66.     unsigned int first() const {
  67.       return 0;
  68.     }
  69.  
  70.     // Return the index of the last element in the array
  71.     unsigned int last() const {
  72.       return _len/_width-1;
  73.     }
  74.  
  75.     // Return the number of bits in the array
  76.     unsigned int length()const{
  77.       return _len;
  78.     }
  79.  
  80.     // Fetch an array element
  81.     unsigned long operator[](unsigned int s) const {
  82.       s*=_width;
  83.       assert(s+_width<=_len);
  84.       unsigned long mask = 0xffffffff >> (_len - _width);
  85.       unsigned long result = _data;
  86.       result >>= s;
  87.       result &= mask;
  88.       return result;
  89.     }
  90.  
  91.     // Possibly deposit a value into an array element.
  92.     //  Defer actual deposit operation to BitField::operator=
  93.     BitField operator[](unsigned int s) {
  94.       return BitField(&_data, s*_width, _width);
  95.     }
  96.  
  97. private:
  98.     unsigned long _data;    // Actual representation of BitArray
  99.     unsigned int _len;      // number of bits in representation
  100.     unsigned int _width;    // number of bits in a single array
  101.         //     element
  102. };
  103.  
  104. #endif
  105.  
  106.